<?php

namespace app\admin\controller\routine;

use app\common\controller\Backend;

/**
 * 店铺管理
 */
class Shop extends Backend
{
    /**
     * Shop模型对象
     * @var object
     * @phpstan-var \app\admin\model\Shop
     */
    protected object $model;

    protected string|array $defaultSortField = 'sid,desc';

    protected string|array $quickSearchField = ['sid'];

    protected array $withJoinTable = ['admin'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\admin\model\Shop();
    }



    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */

     // 
     /**
     * 添加
     */
    public function add(): void
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!$data) {
                $this->error(__('Parameter %s can not be empty', ['']));
            }else{
                $data['token'] = createRandomStr(20);
            }

            $data = $this->excludeFields($data);
            if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                $data[$this->dataLimitField] = $this->auth->id;
            }

            $result = false;
            $this->model->startTrans();
            try {
                // 模型验证  
                if ($this->modelValidate) {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    if (class_exists($validate)) {
                        $validate = new $validate();
                        if ($this->modelSceneValidate) $validate->scene('add');
                        $validate->check($data);
                    }
                }
                $result = $this->model->save($data);
                $this->model->commit();
            } catch (Throwable $e) {
                $this->model->rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success(__('Added successfully'));
            } else {
                $this->error(__('No rows were added'));
            }
        }

        $this->error(__('Parameter error'));
    }
}

// 店铺设置 
// 店铺装修
// 轮播图